package simulation.initiatives;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

import common.Constants;
import common.SimulationSettings;
import common.StepData;

import gui.Presentation;

public class EnginePush extends AbstractEngine {
  protected Presentation presentation;
  protected Timer clock;
  
  public EnginePush(Presentation presentation, SimulationSettings settings) {
    super(settings);
    this.presentation = presentation;
    
    ActionListener stepTask = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
          step();
      }
    };
    
    clock = new Timer(Constants.DEFAULT_DISPLAY_RATE, stepTask);
  }
  
  /**
   * Called for each iteration of calculations
   */
  public void step() {
    super.step();
    StepData data = simEngine.getStepData();
    presentation.updateCellTemperatures(data.getGrid());
    presentation.moveSunPosition(data.getDegrees());
    presentation.updateTimeDisplay(data.getMilliSecs());
  }
  
  public void adjustDisplayRate(int newRate) {
    clock.setDelay(newRate);
  }
    
  public void start() {
    clock.start();
  }
  
  public void pause() {    
    clock.stop();
  }
  
  public void reset() {
    simEngine.reset();
    presentation.reset();
  } 
    
}
